package com.cf.docker.service;

import com.cf.crud.service.AbsBaseService;
import com.cf.docker.dao.ImageDao;
import com.cf.docker.model.Image;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * boot-awm-ImageService
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/6/20 下午 02:49
 */
public class ImageService extends AbsBaseService<Image, ImageDao> {

    @Autowired
    private DockerService dockerService;


    @Override
    @Transactional
    public Image insert(Image bean) {

        final Image insertImage = super.insert(bean);

        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean pullResult = dockerService.pullImage(insertImage.getMachineId(), insertImage.getPath());
                if (!pullResult) {
                    ImageService.this.deleteById(insertImage.getId());
                } else {
                    insertImage.setState("下载成功");
                    ImageService.this.update(insertImage);
                }
            }
        }).start();
        return insertImage;
    }

    @Override
    public Class getTargetClass() {
        return Image.class;
    }


}
